//
//  LZCouponOrderTableCell.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/17.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCouponOrderTableCell.h"
#import "LZOrderModel.h"
#import "LZCustomerUserModel.h"
#import "LZDateFormatterManager.h"

@interface LZCouponOrderTableCell()
@property (weak, nonatomic) IBOutlet UIImageView *bgImageView;
@property (weak, nonatomic) IBOutlet UILabel *orderNumLabel;
@property (weak, nonatomic) IBOutlet UILabel *couponLabel;
@property (weak, nonatomic) IBOutlet UILabel *couponNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *couponIdLabel;
@property (weak, nonatomic) IBOutlet UILabel *couponIdContentLabel;
@property (weak, nonatomic) IBOutlet UILabel *couponAmountLabel;
@property (weak, nonatomic) IBOutlet UILabel *couponAmountContentLabel;
@property (weak, nonatomic) IBOutlet UILabel *customerLabel;
@property (weak, nonatomic) IBOutlet UILabel *costomerContentLabel;
@property (weak, nonatomic) IBOutlet UILabel *userTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *userTimeContentLabel;
@property (weak, nonatomic) IBOutlet UILabel *fundsProvidedLabel;
@property (weak, nonatomic) IBOutlet UILabel *fundsProvidedContentLabel;
@property (weak, nonatomic) IBOutlet UILabel *provisionTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *provisionTimeContentLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *fundsProvidedLabelBottomSupeViewConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *provisionTimeLabelBottomToSupeViewConstraint;

@end

@implementation LZCouponOrderTableCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.contentView.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    
    [self configureUI];
    [self configureDefaultText];
    
    self.couponNameLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 124;
}

- (void)setOrderModel:(LZOrderModel *)orderModel
{
    _orderModel = orderModel;
    
    self.orderNumLabel.text = orderModel.orderNum.length == 0 ? @" " : orderModel.orderNum;
    self.couponNameLabel.text = [orderModel.promotionType isEqualToString:@"1"] ? orderModel.couponsName : orderModel.grouponName;
    self.couponIdContentLabel.text = orderModel.disCountCode;
    self.couponAmountContentLabel.text = [NSString stringWithFormat:@"%.3fBHD",orderModel.discountMoney];
    self.costomerContentLabel.text = orderModel.customerUser.nickname;
    self.userTimeContentLabel.text = [[LZDateFormatterManager shareInstance] stringFromStringYMDHMSHaveLineyyyyMMddHHmmss:orderModel.payTime];
    
    if ([orderModel.state isEqualToString:@"1"]) {//已经结算
        self.provisionTimeLabel.hidden = NO;
        self.provisionTimeContentLabel.hidden = NO;
        self.fundsProvidedContentLabel.text = LZLocalizedString(@"yes", nil);
        self.provisionTimeContentLabel.text = [[LZDateFormatterManager shareInstance] stringFromStringYMDHMSHaveLineyyyyMMddHHmmss:orderModel.settlementTime];
        self.provisionTimeLabelBottomToSupeViewConstraint.priority = 999;
        self.fundsProvidedLabelBottomSupeViewConstraint.priority = 250;

    }else{
        self.provisionTimeLabel.hidden = YES;
        self.provisionTimeContentLabel.hidden = YES;
        self.fundsProvidedContentLabel.text = LZLocalizedString(@"not_yet", nil);
        self.provisionTimeLabelBottomToSupeViewConstraint.priority = 250;
        self.fundsProvidedLabelBottomSupeViewConstraint.priority = 999;
    }
    
}

#pragma mark -
#pragma mark private
- (void)configureDefaultText
{
    self.couponLabel.text = LZLocalizedString(@"coupon", nil);
    self.couponIdLabel.text = LZLocalizedString(@"coupon_id", nil);
    self.couponAmountLabel.text = LZLocalizedString(@"coupon_amount", nil);
    self.customerLabel.text = LZLocalizedString(@"customer", nil);
    self.userTimeLabel.text = LZLocalizedString(@"time_of_use", nil);
    self.fundsProvidedLabel.text = LZLocalizedString(@"funds_provided", nil);
    self.provisionTimeLabel.text = LZLocalizedString(@"provision_time", nil);
    
    self.couponNameLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 57*2 - 10 - self.couponLabel.intrinsicContentSize.width;
    
    self.couponIdContentLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 57*2 - 10 - self.couponIdLabel.intrinsicContentSize.width;
    
    self.couponAmountContentLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 57*2 - 10 - self.couponAmountLabel.intrinsicContentSize.width;
    
    self.costomerContentLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 57*2 - 10 - self.customerLabel.intrinsicContentSize.width;
    
    self.userTimeContentLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 57*2 - 10 - self.userTimeLabel.intrinsicContentSize.width;
    
    self.fundsProvidedContentLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 57*2 - 10 - self.fundsProvidedLabel.intrinsicContentSize.width;
    
    self.provisionTimeContentLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - 57*2 - 10 - self.provisionTimeLabel.intrinsicContentSize.width;
    
}

- (void)configureUI
{
    UIImage *image = [UIImage imageNamed:@"ordercellbg"];
    
    CGFloat top = image.size.height * 0.8;
    CGFloat left = image.size.width * 0;
    CGFloat bottom = image.size.height * 0.2;
    CGFloat right = image.size.width * 0;
    
    UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
    UIImageResizingMode mode = UIImageResizingModeStretch;
    UIImage *newImage = [image resizableImageWithCapInsets:edgeInsets resizingMode:mode];
    
    self.bgImageView.image = newImage;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
